<?php
/**
 * Created by JetBrains PhpStorm.
 * User: porterzhang
 * Date: 13-3-5 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() { }

    function output() {
        global $_G;
        //判断是否有用户登录，如果已经登录则返回已经登录用户信息
        if ($_G['cloud_uid']) {
            $variable = array(
                'code' => 0,
                'messagestr' => lang('plugin/cloud', '1018'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        //判断是否缺少参数
        if (empty($_GET['mobile_num']) || empty($_GET['password']) || empty($_GET['caid'])) {
            $variable = array(
                'code' => 1001,
                'messagestr' => lang('plugin/cloud', '1001'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        if (!empty($_GET['name'])) {
            $user = C::t('#cloud#cloud_users')->fetch_first_by_name($_GET['name'], $_GET['caid']);
            if (!empty($user)) {
                $variable = array(
                    'code' => 5007,
                    'messagestr' => lang('plugin/cloud', '5007'),
                );
                cloud_core::result(cloud_core::variable($variable));
            }
        }
        //检查
        $user = C::t('#cloud#cloud_users')->fetch_first_by_mobile($_GET['mobile_num'], $_GET['caid']);
        if (!empty($user)) {
            $variable = array(
                'code' => 5005,
                'messagestr' => lang('plugin/cloud', '5005'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        $salt = substr(uniqid(rand()), -6);
        $users = array(
            'name' => empty($_GET['name']) ? $_GET['mobile_num'] : $_GET['name'],
            'password' => md5(md5($_GET['password']) . $salt),
            'mobile_num' => $_GET['mobile_num'],
            'tel_num' => '',
            'birthyear' => 0,
            'birthmonth' => 0,
            'birthday' => 0,
            'headphoto' => '',
            'regtime' => TIMESTAMP,
            'lasttime' => TIMESTAMP,
            'salt' => $salt,
            'lastip' => $_G['clientip'],
            'caid' => $_GET['caid'],
            'status' => 1,
            'sex' => empty($_GET['sex']) ? '' : $_GET['sex'],
            'rename' => empty($_GET['rename']) ? '' : $_GET['rename'],
            'address' => empty($_GET['address']) ? '' : $_GET['address'],

        );
        $cuid = C::t('#cloud#cloud_users')->insert($users);
        //如果插入失败，这返回错误信息
        if (empty($cuid)) {
            $variable = array(
                'code' => 5006,
                'messagestr' => lang('plugin/cloud', '5006'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        $users['cuid'] = $cuid;
        //1.得到应用
        $app = C::t('#cloud#cloud_app')->fetch($users['caid']);
        $users['app'] = $app;
        //将注册用户登录
        plugin_cloud::setLoginStatus($users, 'user');

        //正常返回
        $variable = array(
            'messagestr' => lang('plugin/cloud', '1022'),
        );
        cloud_core::result(cloud_core::variable($variable));
    }
}

?>

